package com.jy.adscm.hospital.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hospital.bean.JyHospitalBase;
import com.jy.adscm.hospital.bean.JyHospitalCompeteHospital;
import com.jy.adscm.hospital.bean.JyHospitalEquipment;
import com.jy.adscm.hospital.bean.JyHospitalIntention;
import com.jy.adscm.hospital.service.JyHospitalCompeteHospitalService;
import com.jy.adscm.hospital.support.HospitalSupport;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 竞争对手信息 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2019-01-16
 */
@RestController
@RequestMapping("/adscm/dealer/jyHospitalCompeteHospital")
public class JyHospitalCompeteHospitalController {

    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JyHospitalCompeteHospitalService jyHospitalCompeteHospitalService;

    /**
     * 增加医院的竞争对手信息
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/addCompeteHospital")
    @AdscmLogger("增加医院的竞争对手信息")
    public SimpleCoreResponse addCompeteHospital(@RequestBody JyHospitalCompeteHospital jyHospitalCompeteHospital, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);

        if (token != null){
            JyUser user = token.getUser();
            if (user != null){
                jyHospitalCompeteHospital.setInputUserId(user.getId());
                if (user.getUnitId() == 0){
                    //表示是生产厂商
                    jyHospitalCompeteHospital.setInputUnit(0);
                    jyHospitalCompeteHospital.setInputUnitType(1);
                }else {
                    jyHospitalCompeteHospital.setInputUnit(user.getUnitId());
                    jyHospitalCompeteHospital.setInputUnitType(2);
                }
                jyHospitalCompeteHospital.setInputTime(new Date());
            }
        }

        return new SimpleCoreResponse("200","增加成功",jyHospitalCompeteHospitalService.insert(jyHospitalCompeteHospital));
    }


    /**
     * 查询医院的竞争对手信息
     *
     * @param type 1 ：查询医院自己的，2：查询其他所有经销商的，3：查询当前经销商自己的
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("/queryCompeteHospital/{baseId}/{type}")
    @AdscmLogger("查询医院的竞争对手信息")
    public SimpleCoreResponse queryCompeteHospital(@PathVariable("baseId") Long baseId,@PathVariable("type") int type,Long formId, HttpServletRequest request) throws AdscmSystemException {

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);


        JyHospitalCompeteHospital hospitalCompeteHospital = new JyHospitalCompeteHospital();

        Wrapper<JyHospitalCompeteHospital> wrapper = new HospitalSupport<JyHospitalCompeteHospital>(hospitalCompeteHospital).loadWrapper(baseId, type,formId, token);

        if (wrapper != null){
            List<JyHospitalCompeteHospital> list = jyHospitalCompeteHospitalService.selectList(wrapper);
            if (list == null){
                list = new ArrayList<>();
            }
            return new SimpleCoreResponse("200","查询成功",list);
//            return new SimpleCoreResponse("200","查询成功",jyHospitalCompeteHospitalService.selectList(wrapper));
        }else {
            return new SimpleCoreResponse("200","查询失败，请登录",null);
        }

    }


}

